package tr.gov.tubitak.bilgem.esya.certviewer.viwerasn;

import sun.security.util.ObjectIdentifier;
import sun.security.x509.*;

import java.io.IOException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Created by IntelliJ IDEA.
 * User: ramazan.girgin
 * Date: 12/4/11
 * Time: 4:31 PM
 * To change this template use File | Settings | File Templates.
 */
public class ECertExtensionFactory {
    public ECertExtensionFactory() {
       /*try {
            OIDMap.addAttribute("QCStatementException",QCStatementExtension.getExtensionIdStr(),QCStatementExtension.class);
        } catch (CertificateException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }*/
    }

    private static ECertExtensionFactory instance=null;
    static ECertExtensionFactory getInstance(){
        if(instance ==null){
            instance = new ECertExtensionFactory();
        }
        return instance;
    }

    public List<ECertExtension> getCertExtensions(X509Certificate certificate)
    {
        X509CertImpl impl = null;
        List<ECertExtension> retExtensionList = new ArrayList<ECertExtension>();
        try {
            impl = new X509CertImpl(certificate.getEncoded());
        } catch (CertificateException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            return retExtensionList;
        }

        Set<String> extensionOIDs = certificate.getNonCriticalExtensionOIDs();
        Set<String> criticalExtensionOIDs = certificate.getCriticalExtensionOIDs();
        extensionOIDs.addAll(criticalExtensionOIDs);

        for (String extensionOID : extensionOIDs) {
            try {
                Extension extension = impl.getExtension(new ObjectIdentifier(extensionOID));
                if(extension != null)
                {
                    ECertExtension eExtension = null;
                    if(extension instanceof AuthorityKeyIdentifierExtension)
                    {
                        eExtension = new EAuthorityKeyIdentifierExtension();
                    }
                    else if(extension instanceof KeyUsageExtension)
                    {
                        boolean [] keyUsageValue = certificate.getKeyUsage();
                        eExtension=new EKeyUsage(keyUsageValue);
                    }
                    else if(extension instanceof CRLDistributionPointsExtension)
                    {
                        eExtension = new ECrlDistributionPointsExtension();
                    }
                    else if(extension instanceof AuthorityInfoAccessExtension)
                    {
                        eExtension = new EAuthorityInfoAccessExtesion();
                    }
                    else if(extension instanceof CertificatePoliciesExtension)
                    {
                        eExtension = new ECertificatePoliciesExtension();
                    }
                    else if(extension instanceof BasicConstraintsExtension)
                    {
                        eExtension = new EBasicContraintsExtension();
                    }
                    else if(extension instanceof SubjectKeyIdentifierExtension)
                    {
                        eExtension = new ESubjectKeyIdentifierExtension();
                    }
                    else if(extension instanceof SubjectAlternativeNameExtension)
                    {
                        eExtension = new ESubjectAlternativeNameExtension();
                    }
                    else if(extension instanceof IssuerAlternativeNameExtension)
                    {
                        eExtension = new EIssuerAlternativeNameExtension();
                    }
                    else if(extension instanceof ExtendedKeyUsageExtension)
                    {
                        eExtension = new EExtendedKeyUsageExtension();
                    }
                    else if(extension instanceof QCStatementExtension)
                    {
                        eExtension = new EQCStatementExtension();
                    }

                    else if(extension.getExtensionId().toString().equals(QCStatementExtension.getExtensionIdStr()))
                    {
                        eExtension = new EQCStatementExtension();
                    }
                    else
                    {
                        eExtension = new EUnknownExtension();
                    }
                    eExtension.setAsnObject(extension);
                    retExtensionList.add(eExtension);
                }
            } catch (Exception e) {
                EUnknownExtension eException = new EUnknownExtension(extensionOID,certificate.getExtensionValue(extensionOID));
                retExtensionList.add(eException);
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }
     /*   ObjectIdentifier oid = OIDMap.getOID(QCStatementExtension.getExtensionIdStr());
        oid = null;
        */
        /*try {
            OIDMap.addAttribute("QCStatementException",QCStatementExtension.getExtensionIdStr(),QCStatementExtension.class);
        } catch (CertificateException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } */
        return  retExtensionList;
    }
}
